package com.hb.hb_tblog;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.core.*;

/**
 * Data access object (DAO) for domain model class TbLog.
 * @see com.hb.hb_tblog.TbLog
 * @author MyEclipse - Hibernate Tools
 */
public class TbLogDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(TbLogDAO.class);

	//property constants
	public static final String OPERATE_USER = "operateUser";
	public static final String OPERATE_MODULE = "operateModule";
	public static final String OPERATE_TYPE = "operateType";
	public static final String OPERATE_OBJECT_NAME = "operateObjectName";
	public static final String OPERATE_IP = "operateIp";
	
	Transaction tx=null;
    
    public void save(TbLog transientInstance) {
        log.debug("saving TbLog instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(TbLog persistentInstance) {
        log.debug("deleting TbLog instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public TbLog findById( java.lang.Integer id) {
        log.debug("getting TbLog instance with id: " + id);
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            TbLog instance = (TbLog) getSession()
                    .get("com.hb.hb_tblog.TbLog", id);
            tx.commit();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(TbLog instance) {
        log.debug("finding TbLog instance by example");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            List results = getSession()
                    .createCriteria("com.hb.hb_tblog.TbLog")
                    .add(Example.create(instance))
            .list();
            tx.commit();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding TbLog instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from TbLog as model where model." 
         						+ propertyName + "= ?";
         tx=getSession().getTransaction();
     	tx.begin();
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 tx.commit();
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByOperateUser(Object operateUser) {
		return findByProperty(OPERATE_USER, operateUser);
	}
	
	public List findByOperateModule(Object operateModule) {
		return findByProperty(OPERATE_MODULE, operateModule);
	}
	
	public List findByOperateType(Object operateType) {
		return findByProperty(OPERATE_TYPE, operateType);
	}
	
	public List findByOperateObjectName(Object operateObjectName) {
		return findByProperty(OPERATE_OBJECT_NAME, operateObjectName);
	}
	
	public List findByOperateIp(Object operateIp) {
		return findByProperty(OPERATE_IP, operateIp);
	}
	
    public TbLog merge(TbLog detachedInstance) {
        log.debug("merging TbLog instance");
        try {
        	tx=getSession().getTransaction();
         	tx.begin();
            TbLog result = (TbLog) getSession()
                    .merge(detachedInstance);
            tx.commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(TbLog instance) {
        log.debug("attaching dirty TbLog instance");
        try {
        	tx=getSession().getTransaction();
         	tx.begin();
            getSession().saveOrUpdate(instance);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(TbLog instance) {
        log.debug("attaching clean TbLog instance");
        try {
        	tx=getSession().getTransaction();
         	tx.begin();
            getSession().lock(instance, LockMode.NONE);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}